package cn.sdack.go.auth.controller;

import cn.sdack.go.auth.entity.AccountEntity;
import cn.sdack.go.auth.entity.JsonResult;
import cn.sdack.go.auth.query.RegisterQuery;
import cn.sdack.go.auth.service.AccountService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Pattern;

/**
 * @author sdack
 * @date 2024/1/10
 */
@RestController
public class AccountController {

    @Autowired
    private AccountService accountService;

    public final String REGEX_EMAIL  = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";

    @GetMapping("/createSimple")
    public JsonResult createSimple(@RequestParam(value = "m",defaultValue = "0") Long mobile) {
        if (mobile < 10000000000L) {
            return JsonResult.toJson(false);
        }
        try {
            AccountEntity entity = accountService.createSimpleAccount(mobile);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("account",entity.getAccount());
            jsonObject.put("avatar",entity.getAvatar());
            jsonObject.put("nickname",entity.getNickname());
            return JsonResult.toJson(jsonObject);
        } catch (Exception e) {
            String message = e.getMessage();
            if (message.length() > 100) {
                message = message.substring(0, 80);
            }
            return JsonResult.toJson(false, message);
        }
    }

    /**
     * 创建账户
     * @return
     */
    @PostMapping("/register/account")
    public JsonResult create(RegisterQuery param) {
        if ((param.getMobile() == null || param.getMobile() < 1000000000) && !StringUtils.hasLength(param.getEmail())) {
            return JsonResult.toJson(false, "手机或邮箱 必填其一");
        }
        if (StringUtils.hasLength(param.getPassword())) {
            if (param.getPassword().length() < 6) {
                return JsonResult.toJson(false, "密码长度太短");
            }
        }
        if (StringUtils.hasLength(param.getEmail())) {
            if (!Pattern.matches(REGEX_EMAIL,param.getEmail())) {
                return JsonResult.toJson(false, "邮箱格式不正确");
            }
        }
        try {
            AccountEntity entity = accountService.createAccount(param);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("account",entity.getAccount());
            jsonObject.put("avatar",entity.getAvatar());
            jsonObject.put("nickname",entity.getNickname());
            return JsonResult.toJson(jsonObject);
        } catch (Exception e) {
            String message = e.getMessage();
            if (message.length() > 100) {
                message = message.substring(0, 80);
            }
            return JsonResult.toJson(false, message);
        }
    }

    @PostMapping("/setUserinfo")
    public JsonResult setUserinfo(AccountEntity param) {
        if (param.getAccount() == null) {
            return JsonResult.toJson(false, "账号必填");
        }
        try {
            accountService.updateUserinfo(param);
            return JsonResult.toJson(true);
        } catch (Exception e) {
            String message = e.getMessage();
            if (message.length() > 100) {
                message = message.substring(0, 80);
            }
            return JsonResult.toJson(false, message);
        }
    }

    @GetMapping("/account/lock")
    public JsonResult<?> lock(@RequestParam("a") String account, @RequestParam(value = "l",defaultValue = "0") Integer lock) {
        try {
            accountService.setLock(account,lock);
            return JsonResult.toJson(true);
        } catch (Exception e) {
            String message = e.getMessage();
            if (message.length() > 100) {
                message = message.substring(0, 80);
            }
            return JsonResult.toJson(false, message);
        }
    }

}
